package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @create: 2024/8/28
 * @author: gyhe10
 * @description: elasticsearch 配置类
 **/

@Configuration
public class EsConfig {

    // 创建和配置一个包含通用 HTTP 请求选项的 RequestOptions 对象，以便在应用中重复使用
    public static final RequestOptions COMMON_OPTIONS;
    static {
        // 创建一个可配置的 RequestOptions.Builder 对象
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();

        // 设置请求头
        // builder.addHeader("Authorization", "Basic ZWxhc3RpYzpwYXNzd29yZA==");

        // 设置异步响应的缓冲区大小
        /*builder.addHttpAsyncResponseConsumerFactory(
                new HttpAsyncResponseConsumerFactory
                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024)
        );*/

        // 构建 RequestOptions 对象
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestClient() {
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.101", 9200, "http")
                        // 如果有多个节点，可以继续添加
                        // new HttpHost("192.168.200.101", 9201, "http")
                )
        );
    }
}
